{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Missing Ranges"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #array"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #数组"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: findMissingRanges"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #缺失的区间"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给你一个闭区间&nbsp;<code>[lower, upper]</code> 和一个 <strong>按从小到大排序</strong> 的整数数组 <code>nums</code><em><strong>&nbsp;</strong></em>，其中元素的范围在闭区间&nbsp;<code>[lower, upper]</code>&nbsp;当中。</p>\n",
    "\n",
    "<p>如果一个数字 <code>x</code> 在 <code>[lower, upper]</code>&nbsp;区间内，并且 <code>x</code> 不在 <code>nums</code> 中，则认为 <code>x</code> <strong>缺失</strong>。</p>\n",
    "\n",
    "<p>返回&nbsp;<strong>准确涵盖所有缺失数字&nbsp;</strong>的 <strong>最小排序</strong> 区间列表。也就是说，<code>nums</code> 的任何元素都不在任何区间内，并且每个缺失的数字都在其中一个区间内。</p>\n",
    "&nbsp;\n",
    "\n",
    "<p><strong class=\"example\">示例 1：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入: </strong>nums = <code>[0, 1, 3, 50, 75]</code>, lower = 0 , upper = 99\n",
    "<strong>输出: </strong>[[2,2],[4,49],[51,74],[76,99]]\n",
    "<strong>解释：</strong>返回的区间是：\n",
    "[2,2]\n",
    "[4,49]\n",
    "[51,74]\n",
    "[76,99]</pre>\n",
    "\n",
    "<p><strong class=\"example\">示例 2：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong> nums = [-1], lower = -1, upper = -1\n",
    "<strong>输出：</strong> []\n",
    "<b>解释：</b>&nbsp;没有缺失的区间，因为没有缺失的数字。</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>-10<sup>9</sup> &lt;= lower &lt;= upper &lt;= 10<sup>9</sup></code></li>\n",
    "\t<li><code>0 &lt;= nums.length &lt;= 100</code></li>\n",
    "\t<li><code>lower &lt;= nums[i] &lt;= upper</code></li>\n",
    "\t<li><code>nums</code> 中的所有值 <strong>互不相同</strong></li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [missing-ranges](https://leetcode.cn/problems/missing-ranges/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [missing-ranges](https://leetcode.cn/problems/missing-ranges/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['[0,1,3,50,75]\\n0\\n99', '[-1]\\n-1\\n-1']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMissingRanges(self, nums: List[int], lower: int, upper: int) -> List[List[int]]:\n",
    "        low = lower - 1\n",
    "        ans = []\n",
    "        nums.append(upper + 1)\n",
    "        for num in nums:\n",
    "            dif = num - low\n",
    "            if dif == 2:\n",
    "                ans.append([low + 1, low + 1])\n",
    "            elif dif > 2:\n",
    "                ans.append([low+1, num-1])\n",
    "            low = num\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMissingRanges(self, nums: List[int], lower: int, upper: int) -> List[List[int]]:\n",
    "        lr = []\n",
    "        nums.append(upper+1)\n",
    "        low = lower - 1\n",
    "        for i in nums:\n",
    "            cha =  i - low\n",
    "            if cha == 2:\n",
    "                lr.append([low+1,i-1])\n",
    "            elif cha >2:\n",
    "                lr.append([low +1,i-1])\n",
    "            low = i\n",
    "        return lr \n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMissingRanges(self, nums: List[int], lower: int, upper: int) -> List[str]:\n",
    "\n",
    "        nums = [lower-1] + nums + [upper+1]\n",
    "        ans = []\n",
    "        \n",
    "        for i in range(1, len(nums)):\n",
    "            if nums[i] - nums[i-1] > 2:\n",
    "                ans.append([nums[i-1]+1,nums[i]-1])\n",
    "            \n",
    "            elif nums[i] - nums[i-1] == 2:\n",
    "                ans.append([nums[i-1]+1, nums[i-1]+1])\n",
    "                \n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMissingRanges(self, nums: List[int], lower: int, upper: int) -> List[List[int]]:\n",
    "        nums = [lower-1] + nums + [upper+1]\n",
    "        ans = []\n",
    "        \n",
    "        for i in range(1, len(nums)):\n",
    "            if nums[i] - nums[i-1] > 2:\n",
    "                ans.append([nums[i-1]+1,nums[i]-1])\n",
    "            \n",
    "            elif nums[i] - nums[i-1] == 2:\n",
    "                ans.append([nums[i-1]+1,nums[i-1]+1])\n",
    "                \n",
    "        return ans\n",
    "\n",
    "\n",
    "        \n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMissingRanges(self, nums: List[int], lower: int, upper: int) -> List[List[int]]:\n",
    "        nums.append(upper+1)\n",
    "        res = []\n",
    "        last = lower - 1\n",
    "        for num in nums:\n",
    "            if num - last >= 2:\n",
    "                res.append([last+1,num-1])\n",
    "            last = num\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMissingRanges(self, nums: List[int], lower: int, upper: int) -> List[List[int]]:\n",
    "         \n",
    "        ranges = []\n",
    "        start = lower\n",
    "        for num in nums:\n",
    "            if num > start:\n",
    "                if num - 1 == start:\n",
    "                    ranges.append([start, start])\n",
    "                else:\n",
    "                    ranges.append([start, num - 1])\n",
    "            start = num + 1\n",
    "        \n",
    "        if upper >= start:\n",
    "            if upper == start:\n",
    "                ranges.append([start, start])\n",
    "            else:\n",
    "                ranges.append([start, upper])\n",
    "        \n",
    "        return ranges\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMissingRanges(self, nums: List[int], lower: int, upper: int) -> List[List[int]]:\n",
    "\n",
    "        nums_copy = [lower] + nums[:] + [upper]\n",
    "        if not nums:\n",
    "            return [[lower, upper]]\n",
    "        res = list()\n",
    "        if nums_copy[1] - nums_copy[0] >= 1:\n",
    "            res.append([nums_copy[0] , nums_copy[1]-1])\n",
    "\n",
    "        for i in range(2, len(nums_copy)-1):\n",
    "            if nums_copy[i] - nums_copy[i-1] > 1:\n",
    "                res.append([nums_copy[i-1]+1,  nums_copy[i]-1])\n",
    "\n",
    "        if nums_copy[-1] - nums_copy[-2] >= 1:\n",
    "            res.append([nums_copy[-2]+1,  nums_copy[-1]])\n",
    "\n",
    "\n",
    "\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMissingRanges(self, nums: List[int], lower: int, upper: int) -> List[List[int]]:\n",
    "        if not nums:\n",
    "            return [[lower,upper]]\n",
    "        \n",
    "        res = []\n",
    "        for n in nums:\n",
    "            r = n\n",
    "            if r > lower:\n",
    "                res.append([lower,r-1])\n",
    "            lower = r + 1\n",
    "        if n < upper:\n",
    "            res.append([lower,upper])\n",
    "        return res\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMissingRanges(self, nums: List[int], lower: int, upper: int) -> List[List[int]]:\n",
    "        if not nums:\n",
    "            return [[lower,upper]]\n",
    "        last=lower\n",
    "        ret=[]\n",
    "        nums.sort()\n",
    "        for n in nums:\n",
    "            if n==last:\n",
    "                last+=1\n",
    "            else:\n",
    "                ret.append([last,n-1])\n",
    "                last=n+1\n",
    "        if last<=upper:\n",
    "            ret.append([last,upper])\n",
    "        return ret"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMissingRanges(self, nums, lower, upper):\n",
    "        result = []\n",
    "        if not nums:\n",
    "            result.append([lower, upper])\n",
    "            return result\n",
    "        left = lower\n",
    "        n = 0\n",
    "        while n < len(nums):\n",
    "            if left < nums[0]:\n",
    "                result.append([left, nums[0]-1])\n",
    "                left = nums[0]\n",
    "                continue\n",
    "            if left+1 in nums:\n",
    "                n += 1\n",
    "                left = nums[n]\n",
    "                continue\n",
    "            left = nums[n] + 1\n",
    "            n += 1\n",
    "            if n > len(nums)-1:\n",
    "                break\n",
    "            right = nums[n]\n",
    "            result.append([left, right - 1])\n",
    "            left = nums[n]\n",
    "        if nums[-1] < upper:\n",
    "            result.append([nums[-1]+1, upper])\n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMissingRanges(self, nums: List[int], lower: int, upper: int) -> List[List[int]]:\n",
    "        res = []\n",
    "        for i in range(len(nums)):\n",
    "            if nums[i] > lower:\n",
    "                res.append([lower,nums[i]-1])\n",
    "            lower = nums[i] + 1\n",
    "        if lower <= upper:\n",
    "            res.append([lower,upper])\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMissingRanges(self, nums: List[int], lower: int, upper: int) -> List[List[int]]:\n",
    "        ranges = []\n",
    "        if len(nums) == 0:\n",
    "            return [[lower,upper]]\n",
    "        if nums[0] != lower:\n",
    "            ranges.append([lower,nums[0]-1])\n",
    "        for i in range(len(nums)-1):\n",
    "            if nums[i+1] - nums[i] > 1:\n",
    "                ranges.append([nums[i]+1,nums[i+1]-1])\n",
    "        if nums[-1] != upper:\n",
    "            ranges.append([nums[-1]+1,upper])\n",
    "        return ranges"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMissingRanges(self, nums: List[int], lower: int, upper: int) -> List[List[int]]:\n",
    "        lr = []\n",
    "        nums.append(upper+1)\n",
    "        low = lower - 1\n",
    "        for i in nums:\n",
    "            cha =  i - low\n",
    "            if cha >= 2:\n",
    "                lr.append([low+1,i-1])\n",
    "            low = i\n",
    "        return lr \n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMissingRanges(self, nums: List[int], lower: int, upper: int) -> List[List[int]]:\n",
    "\n",
    "        res = []\n",
    "        for num in nums:\n",
    "            if lower < num:\n",
    "                res.append([lower, num - 1])\n",
    "            \n",
    "            lower = num + 1\n",
    "        if lower <= upper:\n",
    "            res.append([lower, upper])\n",
    "\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMissingRanges(self, nums: List[int], lower: int, upper: int) -> List[List[int]]:\n",
    "        list1 = []\n",
    "        if len(nums) != 0:\n",
    "            if nums[0] > lower :\n",
    "                list1.append([lower,nums[0]-1])\n",
    "            for i in range(len(nums)-1):\n",
    "                if nums[i] < lower and nums[i+1] > lower + 1:\n",
    "                    list1.append([lower,nums[i+1]])\n",
    "                elif lower <= nums[i] <nums[i+1]<= upper:\n",
    "                    if nums[i+1] - nums[i] != 1:\n",
    "                        list1.append([nums[i]+1,nums[i+1]-1])\n",
    "                elif nums[i] < upper - 1 and nums[i+1] > lower:\n",
    "                    list1.append([nums[i],upper])\n",
    "            if nums[-1] < upper :\n",
    "                list1.append([nums[-1]+1,upper])\n",
    "        else:\n",
    "            list1.append([lower,upper])\n",
    "        return list1\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMissingRanges(self, nums: List[int], lower: int, upper: int) -> List[List[int]]:\n",
    "        res = []\n",
    "        low = lower - 1\n",
    "        nums.append(upper + 1)\n",
    "        for num in nums:\n",
    "            dif = num - low\n",
    "            if dif == 2: res.append([low+1, low+1])\n",
    "            elif dif > 2: res.append([low+1, num-1])\n",
    "            low = num\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMissingRanges(self, nums: List[int], lower: int, upper: int) -> List[List[int]]:\n",
    "        list1 = []\n",
    "        if len(nums) != 0:\n",
    "            if nums[0] > lower :\n",
    "                list1.append([lower,nums[0]-1])\n",
    "            for i in range(len(nums)-1):\n",
    "                if nums[i] < lower and nums[i+1] > lower + 1:\n",
    "                    list1.append([lower,nums[i+1]])\n",
    "                elif lower <= nums[i] <nums[i+1]<= upper:\n",
    "                    if nums[i+1] - nums[i] != 1:\n",
    "                        list1.append([nums[i]+1,nums[i+1]-1])\n",
    "                elif nums[i] < upper - 1 and nums[i+1] > lower:\n",
    "                    list1.append([nums[i],upper])\n",
    "            if nums[-1] < upper :\n",
    "                list1.append([nums[-1]+1,upper])\n",
    "        else:\n",
    "            list1.append([lower,upper])\n",
    "        return list1\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMissingRanges(self, nums: list[int], lower: int, upper: int) -> list[List[int]]:\n",
    "        nums.insert(0, lower - 1)\n",
    "        nums.append(upper + 1)\n",
    "        new_nums = []\n",
    "        for i in range(len(nums) - 1):\n",
    "            n = nums[i + 1] - nums[i]\n",
    "            if n > 1:\n",
    "                new_nums.append([nums[i] + 1, nums[i + 1] - 1])\n",
    "        return new_nums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMissingRanges(self, nums: List[int], lower: int, upper: int) -> List[List[int]]:\n",
    "        res = []\n",
    "        for num in nums: \n",
    "            if lower<num:\n",
    "                res.append([lower,num-1])\n",
    "            lower = num+1\n",
    "        if len(nums) and nums[-1]<upper: \n",
    "            res.append([nums[-1]+1,upper])\n",
    "        if not len(nums):\n",
    "            res.append([lower,upper])\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMissingRanges(self, nums: List[int], lower: int, upper: int) -> List[List[int]]:\n",
    "        results = []\n",
    "        if len(nums)==0:\n",
    "            results.append([lower, upper])\n",
    "            return results\n",
    "        if nums[0]>lower:\n",
    "            results.append([lower, nums[0]-1])\n",
    "        n = len(nums)\n",
    "        for i in range(n-1):\n",
    "            if nums[i+1]>nums[i]+1:\n",
    "                results.append([nums[i]+1, nums[i+1]-1])\n",
    "        if nums[-1]<upper:\n",
    "            results.append([nums[-1]+1, upper])\n",
    "        return results\n",
    "                \n",
    "            \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMissingRanges(self, nums: List[int], lower: int, upper: int) -> List[List[int]]:\n",
    "        lr = []\n",
    "        nums.append(upper+1)\n",
    "        low = lower - 1\n",
    "        for i in nums:\n",
    "            cha =  i - low\n",
    "            if cha >= 2:\n",
    "                lr.append([low+1,i-1])\n",
    "            low = i\n",
    "        return lr \n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMissingRanges(self, nums: List[int], lower: int, upper: int) -> List[List[int]]:\n",
    "        nums.sort()\n",
    "        nums.insert(0,lower-1)\n",
    "        nums.append(upper+1)\n",
    "        res=[]\n",
    "        for i in range(1,len(nums)):\n",
    "            if nums[i]>nums[i-1]+1:\n",
    "                res.append([nums[i-1]+1,nums[i]-1])\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMissingRanges(self, nums: List[int], lower: int, upper: int) -> List[List[int]]:\n",
    "        ans = []\n",
    "\n",
    "        nums.append(upper + 1)\n",
    "        now = lower - 1\n",
    "        for n in nums:\n",
    "            if n - now == 2:\n",
    "                ans.append([n-1,n-1])\n",
    "            elif n - now > 2:\n",
    "                ans.append([now+1,n-1])\n",
    "            now = n\n",
    "\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMissingRanges(self, nums: List[int], lower: int, upper: int) -> List[List[int]]:\n",
    "        \n",
    "        if not nums:\n",
    "            return [[lower, upper]]\n",
    "\n",
    "        result = []\n",
    "        i = 0\n",
    "        \n",
    "        if nums[i] > lower:\n",
    "            result.append([lower, nums[i] - 1])\n",
    "\n",
    "        while i < len(nums) - 1:\n",
    "            if nums[i + 1] > nums[i] + 1:\n",
    "                result.append([nums[i] + 1, nums[i + 1] -1])\n",
    "            i += 1\n",
    "\n",
    "        if nums[i] < upper:\n",
    "            result.append([nums[i] + 1, upper])\n",
    "        \n",
    "        return result\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMissingRanges(self, nums: List[int], lower: int, upper: int) -> List[List[int]]:\n",
    "        if not nums:\n",
    "            return [[lower, upper]]\n",
    "        ans = []\n",
    "        last = lower\n",
    "        for num in nums:\n",
    "            if last != num:\n",
    "                ans.append([last, num - 1])\n",
    "            last = num + 1\n",
    "        if last <= upper and (not ans or ans[-1][-1] < upper):\n",
    "            ans.append([last, upper])\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMissingRanges(self, nums: List[int], lower: int, upper: int) -> List[List[int]]:\n",
    "\n",
    "        nums_copy = [lower] + nums[:] + [upper]\n",
    "        if not nums:\n",
    "            return [[lower, upper]]\n",
    "        res = list()\n",
    "        if nums_copy[1] - nums_copy[0] >= 1:\n",
    "            res.append([nums_copy[0] , nums_copy[1]-1])\n",
    "\n",
    "        for i in range(2, len(nums_copy)-1):\n",
    "            if nums_copy[i] - nums_copy[i-1] > 1:\n",
    "                res.append([nums_copy[i-1]+1,  nums_copy[i]-1])\n",
    "\n",
    "        if nums_copy[-1] - nums_copy[-2] >= 1:\n",
    "            res.append([nums_copy[-2]+1,  nums_copy[-1]])\n",
    "\n",
    "\n",
    "\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMissingRanges(self, nums: List[int], lower: int, upper: int) -> List[List[int]]:\n",
    "        if len(nums) == 0:\n",
    "            return [[lower, upper]]\n",
    "        res = []\n",
    "        low = lower\n",
    "        nums.append(upper+1)\n",
    "        if lower < nums[0]:\n",
    "            res.append([lower, nums[0]-1])\n",
    "            low = nums[0]\n",
    "        for n in nums:\n",
    "            if n - 2 - low >= 0:\n",
    "                res.append([low+1, n-1])\n",
    "            low = n\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMissingRanges(self, nums: List[int], lower: int, upper: int) -> List[List[int]]:\n",
    "        if len(nums) == 0:\n",
    "            return [[lower,upper]]\n",
    "        output_list = []\n",
    "        i = lower\n",
    "        pointer = 0\n",
    "        while(i <= upper) and pointer < len(nums):\n",
    "            if i not in nums:\n",
    "                output_list.append([i,nums[pointer]-1])\n",
    "                i = nums[pointer]\n",
    "            else:\n",
    "                i += 1\n",
    "                pointer += 1\n",
    "        if nums[pointer-1] != upper:\n",
    "            output_list.append([i,upper])\n",
    "        return output_list\n",
    "                \n",
    "                "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMissingRanges(self, nums: List[int], lower: int, upper: int) -> List[List[int]]:\n",
    "        # 暴力:\n",
    "        if not nums:\n",
    "            return [[lower, upper]]\n",
    "        if nums[0] >= upper and upper > lower:\n",
    "            return [[lower, upper-1]]\n",
    "\n",
    "\n",
    "        res = []\n",
    "        if nums[0] > lower:\n",
    "            if nums[0] - lower >= 1:\n",
    "                res.append([lower, nums[0]-1])\n",
    "        for i in range(1, len(nums)):\n",
    "            if nums[i] <= upper and nums[i-1] >= lower:\n",
    "                if nums[i]-nums[i-1] > 1:\n",
    "                    res.append([nums[i-1]+1, nums[i]-1])\n",
    "        if nums[-1] < upper:\n",
    "            res.append([nums[-1]+1, upper])\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMissingRanges(self, nums: List[int], lower: int, upper: int) -> List[List[int]]:\n",
    "        lr = []\n",
    "        nums.append(upper+1)\n",
    "        low = lower - 1\n",
    "        for i in nums:\n",
    "            cha =  i - low\n",
    "            if cha >= 2:\n",
    "                lr.append([low+1,i-1])\n",
    "            low = i\n",
    "        return lr \n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMissingRanges(self, nums: List[int], lower: int, upper: int) -> List[List[int]]:\n",
    "        intervals = []\n",
    "        if not nums:\n",
    "            return [[lower, upper]]\n",
    "        lower -=1\n",
    "        for i in range(len(nums)):\n",
    "            lower += 1\n",
    "            if nums[i] == lower:\n",
    "                continue\n",
    "            else:\n",
    "                intervals.append([lower, nums[i]-1])\n",
    "                lower = nums[i]\n",
    "        if nums[-1] != upper:\n",
    "            intervals.append([nums[i]+1, upper])\n",
    "        return intervals"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMissingRanges(self, nums: List[int], lower: int, upper: int) -> List[List[int]]:\n",
    "        if not nums:\n",
    "            return [[lower, upper]]\n",
    "        res = []\n",
    "        if lower < nums[0]:\n",
    "            res.append([lower, nums[0]-1])\n",
    "        pre = nums[0]\n",
    "        for i in range(1, len(nums)):\n",
    "            num = nums[i]\n",
    "            if upper >= num > pre+1:\n",
    "                if num > upper:\n",
    "                    res.append([pre+1, upper-1])\n",
    "                else:\n",
    "                    res.append([pre+1, num-1])\n",
    "            pre = num\n",
    "        if upper > pre:\n",
    "            res.append([pre+1, upper])\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMissingRanges(self, nums: List[int], lower: int, upper: int) -> List[List[int]]:\n",
    "        lr = []\n",
    "        nums.append(upper+1)\n",
    "        low = lower - 1\n",
    "        for i in nums:\n",
    "            cha =  i - low\n",
    "            if cha >= 2:\n",
    "                lr.append([low+1,i-1])\n",
    "            low = i\n",
    "        return lr \n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMissingRanges(self, nums: List[int], lower: int, upper: int) -> List[List[int]]:\n",
    "        lr = []\n",
    "        nums.append(upper+1)\n",
    "        low = lower - 1\n",
    "        for i in nums:\n",
    "            cha =  i - low\n",
    "            if cha >= 2:\n",
    "                lr.append([low+1,i-1])\n",
    "            low = i\n",
    "        return lr \n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMissingRanges(self, nums: List[int], lower: int, upper: int) -> List[List[int]]:\n",
    "        list1 = []\n",
    "        if len(nums) != 0:\n",
    "            if nums[0] > lower :\n",
    "                list1.append([lower,nums[0]-1])\n",
    "            for i in range(len(nums)-1):\n",
    "                if nums[i] < lower and nums[i+1] > lower + 1:\n",
    "                    list1.append([lower,nums[i+1]])\n",
    "                elif lower <= nums[i] <nums[i+1]<= upper:\n",
    "                    if nums[i+1] - nums[i] != 1:\n",
    "                        list1.append([nums[i]+1,nums[i+1]-1])\n",
    "                elif nums[i] < upper - 1 and nums[i+1] > lower:\n",
    "                    list1.append([nums[i],upper])\n",
    "            if nums[-1] < upper :\n",
    "                list1.append([nums[-1]+1,upper])\n",
    "        else:\n",
    "            list1.append([lower,upper])\n",
    "        return list1\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMissingRanges(self, nums: List[int], lower: int, upper: int) -> List[List[int]]:\n",
    "        last = float(\"-inf\")\n",
    "        \n",
    "        res = []\n",
    "        for n in nums:\n",
    "            r = n\n",
    "            if r > lower:\n",
    "                res.append([lower,r-1])\n",
    "            lower = r + 1\n",
    "            last = n\n",
    "        if last < upper:\n",
    "            res.append([lower,upper])\n",
    "        return res\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMissingRanges(self, nums: List[int], lower: int, upper: int) -> List[List[int]]:\n",
    "        nums = [lower-1] + nums + [upper +1] # -1 +1 must have. otherwise miss the missing value on the upper or lower\n",
    "        res = []\n",
    "        n = len(nums)\n",
    "        for i in range(1, n):\n",
    "            if nums[i] - nums[i-1] == 2:\n",
    "                res.append([nums[i]-1, nums[i]-1])\n",
    "            elif nums[i] - nums[i-1] > 2:\n",
    "                res.append([nums[i-1] + 1, nums[i] -1])\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMissingRanges(self, nums: List[int], lower: int, upper: int) -> List[List[int]]:\n",
    "        # res= []\n",
    "        # if nums == []: return [[lower,upper]]\n",
    "        # if len(nums) == 1 and lower != upper: \n",
    "        #     if nums[0] == lower:\n",
    "        #         return [[lower+1,upper]]\n",
    "        #     elif nums[0] == upper:\n",
    "        #         return [[lower,upper-1]]\n",
    "        #     else:\n",
    "        #         return [[lower, nums[0]-1],[nums[0]+1,upper]]\n",
    "        # elif len(nums) == 1 and lower == upper:\n",
    "        #     return res\n",
    "        \n",
    "\n",
    "        # if lower - nums[0] <= -1: res.append([lower, nums[0]-1])\n",
    "        # for i in range(len(nums)-1):\n",
    "        #     if nums[i] > upper or nums[i+1] < lower:\n",
    "        #         break\n",
    "        #     elif nums[i] - nums[i+1] == -1:\n",
    "        #         continue\n",
    "        #     else:\n",
    "        #         res.append([max(lower,nums[i])+1, min(upper, nums[i+1])-1])\n",
    "        \n",
    "        # if upper - nums[-1] >= 1: res.append([nums[-1]+1, upper])\n",
    "        # return res\n",
    "        res=[]\n",
    "        low = lower -1 \n",
    "        nums.append(upper + 1)\n",
    "        for num in nums:\n",
    "            dif = num - low\n",
    "            if dif ==2:\n",
    "                res.append([low+1, low+1])\n",
    "                print([low+1, low+1])\n",
    "            elif dif >2:\n",
    "                res.append([low+1, num-1])\n",
    "                print([low+1, num-1])\n",
    "            low = num\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "\n",
    "    def findMissingRanges(self, nums: List[int], lower: int, upper: int) -> List[List[int]]:\n",
    "        res = []\n",
    "        low = lower-1\n",
    "        nums.append(upper+1)\n",
    "        for num in nums:\n",
    "            dif = num-low\n",
    "            if dif==2: res.append([low+1, low+1])\n",
    "            elif dif>2: res.append([low+1, num-1])\n",
    "            low = num\n",
    "        return res\n",
    "\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMissingRanges(self, nums: List[int], lower: int, upper: int) -> List[List[int]]:\n",
    "        ret=[]\n",
    "        left=lower\n",
    "        item=lower\n",
    "        for item in nums:\n",
    "            if item>upper:\n",
    "                break\n",
    "            if item!=left:\n",
    "                ret.append([left,item-1])\n",
    "            left=item+1\n",
    "        if left<=upper:\n",
    "            ret.append([left,upper])\n",
    "        return ret"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMissingRanges(self, nums: List[int], lower: int, upper: int) -> List[List[int]]:\n",
    "        n = len(nums)\n",
    "        if n == 0:\n",
    "            return [[lower, upper]]\n",
    "        ans = []\n",
    "        if nums[0] > lower:\n",
    "            ans.append([lower, nums[0] - 1])\n",
    "        for a, b in pairwise(nums):\n",
    "            if b - a > 1:\n",
    "                ans.append([a + 1, b - 1])\n",
    "        if nums[-1] < upper:\n",
    "            ans.append([nums[-1] + 1, upper])\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMissingRanges(self, nums: List[int], lower: int, upper: int) -> List[List[int]]:\n",
    "        ranges = []\n",
    "        if len(nums) == 0:\n",
    "            ranges.append([lower,upper])\n",
    "            return ranges\n",
    "        if lower != nums[0]:\n",
    "            ranges.append([lower,nums[0]-1])\n",
    "        for i in range(len(nums)):\n",
    "            if i+1 < len(nums) and nums[i] != nums[i+1]-1:\n",
    "                ranges.append([nums[i]+1,nums[i+1]-1])\n",
    "        if upper != nums[-1]:\n",
    "            ranges.append([nums[-1]+1,upper])\n",
    "        return ranges"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMissingRanges(self, nums: List[int], lower: int, upper: int) -> List[List[int]]:\n",
    "        if not nums: return [[lower, upper]]\n",
    "        res = []\n",
    "        pre = lower\n",
    "        n = len(nums)\n",
    "        for i in range(n):\n",
    "            if pre != nums[i]:\n",
    "                res.append([pre, nums[i] - 1])\n",
    "                pre = nums[i] + 1\n",
    "            elif pre == nums[i]:\n",
    "                pre += 1\n",
    "            if i == n - 1 and nums[i] != upper:\n",
    "                res.append([nums[i] + 1, upper])\n",
    "\n",
    "\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMissingRanges(self, nums: List[int], lower: int, upper: int) -> List[List[int]]:\n",
    "        if not nums: return [[lower, upper]]\n",
    "        res = []\n",
    "        pre = lower\n",
    "        n = len(nums)\n",
    "        for i in range(n):\n",
    "            if pre != nums[i]:\n",
    "                res.append([pre, nums[i] - 1])\n",
    "                pre = nums[i] + 1\n",
    "            elif pre == nums[i]:\n",
    "                pre += 1\n",
    "            if i == n - 1 and nums[i] != upper:\n",
    "                res.append([nums[i] + 1, upper])\n",
    "\n",
    "\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMissingRanges(self, nums: List[int], lower: int, upper: int) -> List[List[int]]:\n",
    "        if not len(nums):\n",
    "            return [[lower, upper]]\n",
    "        ans = []\n",
    "        i = 0\n",
    "        while i < len(nums) and nums[i] < lower:\n",
    "            i += 1\n",
    "        if lower < nums[i]:\n",
    "            ans.append([lower, nums[i] - 1])\n",
    "        l, r = i, i\n",
    "        while r < upper:\n",
    "            while i + 1 < len(nums) and nums[i + 1] == nums[i] + 1:\n",
    "                i += 1\n",
    "            if i + 1 == len(nums):\n",
    "                break\n",
    "            l, r = nums[i], nums[i + 1]\n",
    "            ans.append([l + 1, r - 1])\n",
    "            i += 1\n",
    "        if nums[i] < upper:\n",
    "            ans.append([nums[i] + 1, upper])\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMissingRanges(self, nums: List[int], lower: int, upper: int) -> List[List[int]]:\n",
    "\n",
    "        res = []\n",
    "        for n in nums:\n",
    "            r = n\n",
    "            if r > lower:\n",
    "                res.append([lower,r-1])\n",
    "            lower = r + 1\n",
    "        last = nums[-1] if nums else float(\"-inf\")\n",
    "        if last < upper:\n",
    "            res.append([lower,upper])\n",
    "        return res\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from typing import List\n",
    "\n",
    "class Solution:\n",
    "    def findMissingRanges(self, nums: List[int], lower: int, upper: int) -> List[List[int]]:\n",
    "        if not nums:\n",
    "            return [[lower, upper]]\n",
    "        res = []\n",
    "        cur = []\n",
    "        \n",
    "        for i, elm in enumerate(nums):\n",
    "            if i == 0:\n",
    "                if elm > lower:\n",
    "                    cur.append(lower)\n",
    "                    cur.append(elm - 1)\n",
    "                    res.append(cur)\n",
    "            else:\n",
    "                if elm - nums[i - 1] > 1:\n",
    "                    cur = []\n",
    "                    cur.append(nums[i - 1] + 1)\n",
    "                    cur.append(elm - 1)\n",
    "                    res.append(cur)\n",
    "        if elm < upper:\n",
    "            cur = []\n",
    "            cur.append(elm + 1)\n",
    "            cur.append(upper)\n",
    "            res.append(cur)\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "  def findMissingRanges(self, nums: list[int], lower: int, upper: int) -> list[list[int]]:\n",
    "    ans=[]\n",
    "    #首先判断nums是否为[]\n",
    "    if(nums==[]):\n",
    "      return [[lower,upper]]\n",
    "    #对于nums首位，将其于lower判断，若差大于等于2，则为一个缺失区间\n",
    "    if(nums[0]-lower>=1):\n",
    "      ans.append([lower,nums[0]-1])\n",
    "    #中间项依次与前一项判断\n",
    "    i=1\n",
    "    while(i<len(nums)):\n",
    "      if(nums[i]-nums[i-1]>=2):\n",
    "        ans.append([nums[i-1]+1,nums[i]-1])\n",
    "      i+=1\n",
    "    #对于末位，将upper与其判断\n",
    "    if(upper-nums[i-1]>=1):\n",
    "      ans.append([nums[i-1]+1,upper])\n",
    "    return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMissingRanges(self, nums: List[int], lower: int, upper: int) -> List[List[int]]:\n",
    "        ret = []\n",
    "        pre = lower\n",
    "        nums.append(upper + 1)\n",
    "        for num in nums:\n",
    "            u = min(num - 1, upper)\n",
    "            if pre <= u:\n",
    "                ret.append((pre, u))\n",
    "            pre = num + 1\n",
    "        return ret"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMissingRanges(self, nums: List[int], lower: int, upper: int) -> List[List[int]]:\n",
    "        if not nums:\n",
    "            return [[lower, upper]]\n",
    "        res = []\n",
    "        if lower < nums[0]:\n",
    "            res.append([lower, nums[0] - 1])\n",
    "        for i in range(len(nums) - 1):\n",
    "            if nums[i] < nums[i+1] - 1:\n",
    "                res.append([nums[i] + 1, nums[i+1] - 1])\n",
    "        if nums[-1] < upper:\n",
    "            res.append([nums[-1] + 1, upper])\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMissingRanges(self, nums: List[int], lower: int, upper: int) -> List[List[int]]:\n",
    "        res = []\n",
    "        nums.append(upper+1)\n",
    "        low = lower - 1\n",
    "        for num in nums:\n",
    "            diff = num - low\n",
    "            if diff > 2:\n",
    "                res.append([low+1, num-1])\n",
    "            elif diff == 2:\n",
    "                res.append([low+1, low+1])\n",
    "            low = num\n",
    "        return res\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMissingRanges(self, nums: List[int], lower: int, upper: int) -> List[List[int]]:\n",
    "        return ([[lower,nums[0]-1]] if nums[0]>lower else [])+[[nums[i-1]+1,nums[i]-1] for i in range(1,len(nums)) if nums[i]-nums[i-1]>1] + ([[nums[-1]+1,upper]] if upper>nums[-1] else []) if len(nums)>0 else [[lower,upper]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMissingRanges(self, nums: List[int], lower: int, upper: int) -> List[List[int]]:\n",
    "        res = []\n",
    "        if not nums:\n",
    "            return [[lower, upper]]\n",
    "        if lower < nums[0]:\n",
    "            res.append([lower, nums[0]-1])\n",
    "        for i in range(1, len(nums)):\n",
    "            if nums[i] - nums[i-1] > 1:\n",
    "                res.append([nums[i-1] + 1, nums[i] - 1])\n",
    "        if upper > nums[-1]:\n",
    "            res.append([nums[-1] + 1, upper])\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMissingRanges(self, nums: [int], lower: int, upper: int) -> [str]:\n",
    "        res = []\n",
    "        low = lower - 1\n",
    "        nums.append(upper+1)\n",
    "        for x in nums:\n",
    "            diff = x - low\n",
    "            if diff == 2:\n",
    "                res.append([low+1, low+1])\n",
    "            elif diff > 2:\n",
    "                res.append([low+1, x-1])\n",
    "            low = x\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMissingRanges(self, nums: List[int], lower: int, upper: int) -> List[List[int]]:\n",
    "        res = []\n",
    "        low = lower - 1\n",
    "        nums.append(upper + 1)\n",
    "        for num in nums:\n",
    "            dif = num - low\n",
    "            if dif == 2: res.append([low+1, low+1])\n",
    "            elif dif > 2: res.append([low+1, num -1])\n",
    "            low = num\n",
    "        return res\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMissingRanges(self, nums: List[int], lower: int, upper: int) -> List[List[int]]:\n",
    "        res=[]\n",
    "        if nums==[]:\n",
    "            return [[lower,upper]]\n",
    "        if lower==upper:\n",
    "            return res\n",
    "\n",
    "        if  nums[0]-lower>=1:\n",
    "            res.append([lower,nums[0]-1])\n",
    "        for i in range(len(nums)-1):\n",
    "            if nums[i+1]-nums[i]>1:\n",
    "                res.append([nums[i]+1,nums[i+1]-1])\n",
    "        if  upper-nums[-1]>=1:\n",
    "            res.append([nums[-1]+1,upper])\n",
    "        \n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution(object):\n",
    "    def findMissingRanges(self, nums, lower, upper):\n",
    "        res = []\n",
    "        nums = [lower - 1] + nums + [upper + 1]\n",
    "        for i in range(1, len(nums)):\n",
    "            if nums[i] - nums[i - 1] > 2:\n",
    "                res.append([nums[i - 1] + 1, nums[i] - 1])\n",
    "            elif nums[i] - nums[i - 1] == 2:\n",
    "                res.append([nums[i] - 1, nums[i] - 1])\n",
    "        return res\n",
    "            \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMissingRanges(self, nums: List[int], lower: int, upper: int) -> List[List[int]]:\n",
    "\n",
    "        out = []\n",
    "        pre = lower - 1\n",
    "        n = len(nums)\n",
    "\n",
    "        for i in range(n):\n",
    "            num = nums[i]\n",
    "            \n",
    "            if num - 1 >= pre + 1:\n",
    "                out.append([pre+1, num-1])\n",
    "            pre = num\n",
    "        \n",
    "        num = upper + 1\n",
    "        if num - 1 >= pre + 1:\n",
    "            out.append([pre+1, num-1])\n",
    "        \n",
    "        return out\n",
    "        \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMissingRanges(self, nums: List[int], lower: int, upper: int) -> List[List[int]]:\n",
    "        new_nums = [lower - 1] + nums + [upper + 1]\n",
    "        tmp_nums = []\n",
    "        for i in range(len(new_nums) - 1):\n",
    "            num = new_nums[i + 1] - new_nums[i]\n",
    "            if num > 1:\n",
    "                tmp_nums.append([new_nums[i] + 1, new_nums[i + 1] - 1])\n",
    "        return tmp_nums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMissingRanges(self, nums: List[int], lower: int, upper: int) -> List[str]:\n",
    "        a=[]\n",
    "        low=lower-1\n",
    "        nums.append(upper+1)\n",
    "        for num in nums:\n",
    "            diff=num-low\n",
    "            if diff==2:\n",
    "                a.append([num-1,num-1])\n",
    "            elif diff>2:\n",
    "                a.append([low+1,num-1])\n",
    "            low=num\n",
    "        return a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMissingRanges(self, nums: list[int], lower: int, upper: int) -> list[List[int]]:\n",
    "        nums.insert(0, lower - 1)\n",
    "        nums.append(upper + 1)\n",
    "        new_nums = []\n",
    "        for i in range(len(nums) - 1):\n",
    "            n = nums[i + 1] - nums[i]\n",
    "            if n > 1:\n",
    "                new_nums.append([nums[i] + 1, nums[i + 1] - 1])\n",
    "        return new_nums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMissingRanges(self, nums: List[int], lower: int, upper: int) -> List[List[int]]:\n",
    "        res=[]\n",
    "        nums.insert(0,lower-1)\n",
    "        nums.append(upper+1)\n",
    "        for i in range(len(nums)-1):\n",
    "            if nums[i+1]-nums[i]>1:\n",
    "                res.append([nums[i]+1,nums[i+1]-1])\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution(object):\n",
    "    def findMissingRanges(self, nums, lower, upper):\n",
    "        res = []\n",
    "        nums = [lower - 1] + nums + [upper + 1]\n",
    "        for i in range(1, len(nums)):\n",
    "            if nums[i] - nums[i - 1] > 2:\n",
    "                res.append([nums[i - 1] + 1, nums[i] - 1])\n",
    "            elif nums[i] - nums[i - 1] == 2:\n",
    "                res.append([nums[i] - 1, nums[i] - 1])\n",
    "        return res\n",
    "            \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMissingRanges(self, nums: List[int], lower: int, upper: int) -> List[List[int]]:\n",
    "        res = []\n",
    "        if not nums:\n",
    "            return [[lower, upper]]\n",
    "        if lower < nums[0]:\n",
    "            res.append([lower, nums[0]-1])\n",
    "        for i in range(1, len(nums)):\n",
    "            if nums[i] - nums[i-1] > 1:\n",
    "                res.append([nums[i-1] + 1, nums[i] - 1])\n",
    "        if upper > nums[-1]:\n",
    "            res.append([nums[-1] + 1, upper])\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMissingRanges(self, nums: List[int], lower: int, upper: int) -> List[List[int]]:\n",
    "        res = []\n",
    "        n = len(nums)\n",
    "        if n == 0: return [[lower, upper]]\n",
    "\n",
    "        if nums[0] != lower:\n",
    "            res.append([lower, nums[0]-1])\n",
    "\n",
    "        for i in range(1, n):\n",
    "            if nums[i] - nums[i-1] > 1:\n",
    "                res.append([nums[i-1]+1, nums[i]-1])\n",
    "\n",
    "        if nums[n-1] < upper:\n",
    "            res.append([nums[n-1]+1, upper])\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def findMissingRanges(self, nums: List[int], lower: int, upper: int) -> List[List[int]]:\n",
    "\n",
    "        result = []\n",
    "        nums.insert(0, lower-1) \n",
    "        nums.append(upper+1)\n",
    "\n",
    "        for i in range(len(nums)-1):\n",
    "            if nums[i+1] - nums[i] > 1:\n",
    "                result.append([nums[i]+1, nums[i+1]-1])\n",
    "        return result\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMissingRanges(self, nums: List[int], lower: int, upper: int) -> List[List[int]]:\n",
    "        # 添加终止边界\n",
    "        nums.append(upper+1)\n",
    "        ans = []\n",
    "        last = lower-1\n",
    "        for num in nums:\n",
    "            # 比上一个数字中有缺失,需要添加在答案中的\n",
    "            if num - last > 1:\n",
    "                ans.append([last+1, num-1])\n",
    "            last = num\n",
    "        return ans\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMissingRanges(self, nums: List[int], lower: int, upper: int) -> List[List[int]]:\n",
    "        l,r = 0,1\n",
    "        n = len(nums)\n",
    "        if n == 0:\n",
    "            return [[lower,upper]]\n",
    "        res = []\n",
    "        if nums[0] != lower:\n",
    "            res.append([lower,nums[0]-1])\n",
    "        while r < n:\n",
    "            if nums[r] - nums[l] >= 2:\n",
    "                res.append([nums[l]+1,nums[r]-1])\n",
    "            r += 1\n",
    "            l += 1\n",
    "        if upper != nums[n-1]:\n",
    "            res.append([nums[n-1]+1,upper])\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMissingRanges(self, nums: List[int], lower: int, upper: int) -> List[List[int]]:\n",
    "        if not nums: return [[lower, upper]]\n",
    "        res = []\n",
    "        pre = lower\n",
    "        n = len(nums)\n",
    "        for i in range(n):\n",
    "            if pre != nums[i]:\n",
    "                res.append([pre, nums[i] - 1])\n",
    "                pre = nums[i] + 1\n",
    "            elif pre == nums[i]:\n",
    "                pre += 1\n",
    "            if i == n - 1 and nums[i] != upper:\n",
    "                res.append([nums[i] + 1, upper])\n",
    "\n",
    "\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMissingRanges(self, nums: List[int], lower: int, upper: int) -> List[List[int]]:\n",
    "        if not nums:\n",
    "            return [[lower, upper]]\n",
    "        \n",
    "        last_val = lower\n",
    "        idx = 0\n",
    "        ret_list = []\n",
    "\n",
    "        while idx < len(nums):\n",
    "            if nums[idx] != last_val:\n",
    "                ret_list.append([last_val, nums[idx] - 1])\n",
    "            last_val = nums[idx] + 1\n",
    "            idx += 1\n",
    "        if last_val <= upper:\n",
    "            ret_list.append([last_val, upper])\n",
    "        \n",
    "        return ret_list\n",
    "                \n",
    "\n",
    "        \n",
    "\n",
    "            \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# 缺失的区间\n",
    "\n",
    "from typing import List\n",
    "class Solution:\n",
    "    def findMissingRanges(self, nums: List[int], lower: int, upper: int) -> List[List[int]]:\n",
    "        nums.sort()\n",
    "        left = lower\n",
    "        right = lower - 1\n",
    "        res = []\n",
    "        # right >= left\n",
    "        for _ in nums:\n",
    "            if _ > left :\n",
    "                right = _ - 1\n",
    "                res.append([left, right])\n",
    "            left = _ + 1\n",
    "            right = _\n",
    "        if len(nums) >= 0 and left <= upper:\n",
    "            right = upper\n",
    "            res.append([left,right])\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMissingRanges(self, nums, lower, upper):\n",
    "        if not nums:\n",
    "            return [[lower,upper]]\n",
    "        rs = []\n",
    "        if nums[0] > lower:\n",
    "            rs.append([lower, nums[0]-1])\n",
    "            \n",
    "        for i in range(len(nums)-1):\n",
    "            left = nums[i]+1\n",
    "            right = nums[i+1]-1\n",
    "            if left>right:\n",
    "                continue\n",
    "            else:\n",
    "                rs.append([left,right])\n",
    "            \n",
    "        if nums[-1] < upper:\n",
    "            rs.append([nums[-1]+1, upper])\n",
    "        return rs"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
